package org.fortasoft.hatfield.aws;

import java.io.File;
import java.io.IOException;
import java.util.*;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException 
    {
    	 try {
    		 AWSCredentials credentials = new PropertiesCredentials(new File("./auth.properties"));

    		 AmazonEC2Client ec2 = new AmazonEC2Client(credentials);

             DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
             System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() +
                     " Availability Zones.");

             DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
             List<Reservation> reservations = describeInstancesRequest.getReservations();
             Set<Instance> instances = new HashSet<Instance>();

             for (Reservation reservation : reservations) {
                 instances.addAll(reservation.getInstances());
             }

             System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
             
             
             for (Image img: ec2.describeImages().getImages()) {
            	 System.out.println("Image: "+img.getName());
             }
         } catch (AmazonServiceException ase) {
                 System.out.println("Caught Exception: " + ase.getMessage());
                 System.out.println("Reponse Status Code: " + ase.getStatusCode());
                 System.out.println("Error Code: " + ase.getErrorCode());
                 System.out.println("Request ID: " + ase.getRequestId());
         }

    }
}
